<?php

class ContactAdminController extends GxController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('getMessage', 'sendMailTouser'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('inbox','conatcttoadmin'),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionInbox() {
        $this->layout = false;
        $backenduserDetails = '';
        $criteria = new CDbCriteria;
        $criteria->condition = 'from_users_id = ' . getLoginUserId();
        $inboxAll = ContactAdmin::model()->findAll($criteria);
        $backenduserDetails = Profiles::model()->findByAttributes(array('user_id' => Message::ADMIN_ID));
        $this->render('inbox', array('inbox' => $inboxAll, 'backenduserDetails' => $backenduserDetails));
    }

    public function actionsendMailTouser() {
        $this->layout = false;
        if (isset($_POST['msgval'])) {
            $model = new ContactAdmin;
            $model->setAttribute('backend_user_id', SkillConfig::ADMIN_ID);
            $model->setAttribute('subject', 'Admin');
            $model->setAttribute('query', $_POST['msgval']);
            $model->setAttribute('from_users_id', getLoginUserId());
            $model->setAttribute('mail_from', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('is_read', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('created', SkillConfig::CreateDateTime());
            $userdetails = Users::getBackEndUser(SkillConfig::ADMIN_ID);
            $trainername = Users::getFrontEndUser(getLoginUserId());
            $model->save();
            $mailmodel = EmailTemplate::model()->findByPk(Message::CLASS_COURSE_QUERY_NOTIFICATION);
            $userText = $mailmodel->body;
            $mailbody = array('value' => $userText, 'type' => 'raw');
            $userName = $userdetails->first_name . ' ' . $userdetails->last_name;
            $trainernames = $trainername->first_name . ' ' . $trainername->last_name;
            $mailto = $userdetails->user->email;
            $arrayFind = array('[username]', '[from_user_name]', '[classcoursename]', '[classCourseViewdetailsLink]');
            $arrayReplace = array($userName, $trainernames,' ',' ');
            $mailbody = str_replace($arrayFind, $arrayReplace, $mailbody);
            $mailtemp = Template::model()->findByPk($mailmodel->template_id);
            $subject = $mailmodel->subject;
            $body = $this->renderPartial('/mails/' . $mailtemp->filename, array('mailbody' => $mailbody['value']), true, false); 
            try {
                SkillSendMail::SendMail($mailto, $subject, $body);
                $result = 'success';
            } catch (Exception $e) {
                $result = $e;
                exit;
            }
            if ($result == 'success') {
                echo 'success';
            }
            Yii::app()->end();
        }
    }

    public function actionconatcttoadmin() {
        $this->layout = false;
        $dataByUser = ContactAdmin::getMessageCOntactToadmin();
       $adminuser = Users::getBackEndUser(SkillConfig::ADMIN_ID);
        $this->renderPartial('//contactAdmin/contact-to-admin', array('dataByUser' => $dataByUser,'adminuser'=>$adminuser));
    }

}